package toXML;

import houseHold.House;
import houseHold.Room;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.Set;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TransformerHandler;
import javax.xml.transform.stream.StreamResult;

import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;

import RoomPanel.IDGenerator;

public class SaveHouseXML {

	public static void save(File f) throws FileNotFoundException,
			TransformerConfigurationException, SAXException {

		PrintWriter out = new PrintWriter(f);
		StreamResult streamResult = new StreamResult(out);
		SAXTransformerFactory tf = (SAXTransformerFactory) SAXTransformerFactory
				.newInstance();
		TransformerHandler hd = tf.newTransformerHandler();
		Transformer serializer = hd.getTransformer();
		serializer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
		serializer.setOutputProperty(OutputKeys.INDENT, "yes");
		hd.setResult(streamResult);
		hd.startDocument();
		AttributesImpl atts = new AttributesImpl();
		atts.addAttribute("", "", "ROOM_ID", "CDATA", IDGenerator
				.getNextRoomID()
				+ "");
		atts.addAttribute("", "", "OBJECT_ID", "CDATA", IDGenerator
				.getNextObjectID()
				+ "");
		atts.addAttribute("", "", "SENSOR_ID", "CDATA", IDGenerator
				.getNextSensorID()
				+ "");
		hd.startElement("", "", "House", atts);
		Set<String> keySet;
		Iterator<String> it;
		String aux;
		for (int i = 0; i < House.getInstance().getRoomCount(); i++) {
			Room r = House.getInstance().getRoom(i);
			atts.clear();
			atts.addAttribute("", "", "width", "CDATA", r.getWidth() + "");
			atts.addAttribute("", "", "length", "CDATA", r.getLength() + "");
			atts.addAttribute("", "", "height", "CDATA", r.getHeight() + "");
			atts.addAttribute("", "", "Name", "CDATA", r.getName() + "");
			atts.addAttribute("", "", "Type", "CDATA", r.getType() + "");
			hd.startElement("", "", "Room", atts);
			atts.clear();
			atts.addAttribute("", "", "X", "CDATA", r.getOrigin().getX() + "");
			atts.addAttribute("", "", "Y", "CDATA", r.getOrigin().getY() + "");
			hd.startElement("", "", "Origin", atts);
			hd.endElement("", "", "Origin");
			keySet = r.getSensors().keySet();
			it = keySet.iterator();
			while (it.hasNext()) {
				aux = it.next();
				atts.clear();
				atts.addAttribute("", "", "Name", "CDATA", r.getSensor(aux)
						.getSensorName()
						+ "");
				atts.addAttribute("", "", "Type", "CDATA", r.getSensor(aux)
						.getType()
						+ "");
				hd.startElement("", "", "Sensor", atts);
				hd.endElement("", "", "Sensor");
			}
			keySet = r.getItemList().keySet();
			it = keySet.iterator();
			while (it.hasNext()) {
				aux = it.next();
				atts.clear();
				atts.addAttribute("", "", "Name", "CDATA", r.getItem(aux)
						.getName()
						+ "");
				atts.addAttribute("", "", "Type", "CDATA", r.getItem(aux)
						.getType()
						+ "");
				hd.startElement("", "", "Object", atts);
				atts.clear();
				atts.addAttribute("", "", "Consumption", "CDATA", r
						.getItem(aux).getConsumption()
						+ "");
				atts.addAttribute("", "", "ConsumptionType", "CDATA", r
						.getItem(aux).getConsumptionType()
						+ "");
				atts.addAttribute("", "", "Sensor", "CDATA", r.getItem(aux)
						.getSensor().getSensorName()
						+ "");
				hd.startElement("", "", "Consumption", atts);
				hd.endElement("", "", "Consumption");
				hd.endElement("", "", "Object");
			}
			hd.endElement("", "", "Room");
		}
		hd.endElement("", "", "House");
		hd.endDocument();
	}

}
